﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules
    Public NotInheritable Class RuleSetCollection
        Inherits KeyedCollection([Of] String, RuleSet)
        Implements IWorkflowChangeDiff
        ' Methods
        Public Sub Add(ByVal item As RuleSet)
            If Me._runtimeInitialized Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            If (item.Name Is Nothing) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetName, New Object() { "item.Name" }))
            End If
            MyBase.Add(item)
        End Sub

        Public Function Diff(ByVal originalDefinition As Object, ByVal changedDefinition As Object) As IList([Of] WorkflowChangeAction)
            Dim list As New List([Of] WorkflowChangeAction)
            Dim sets As RuleSetCollection = DirectCast(originalDefinition, RuleSetCollection)
            Dim sets2 As RuleSetCollection = DirectCast(changedDefinition, RuleSetCollection)
            If (Not sets2 Is Nothing) Then
                Dim set As RuleSet
                For Each set In sets2
                    If ((Not sets Is Nothing) AndAlso sets.Contains([set].Name)) Then
                        Dim originalRuleSetDefinition As RuleSet = sets.Item([set].Name)
                        If Not originalRuleSetDefinition.Equals([set]) Then
                            list.Add(New UpdatedRuleSetAction(originalRuleSetDefinition, [set]))
                        End If
                    Else
                        list.Add(New AddedRuleSetAction([set]))
                    End If
                Next
            End If
            If (Not sets Is Nothing) Then
                Dim set3 As RuleSet
                For Each set3 In sets
                    If ((sets2 Is Nothing) OrElse Not sets2.Contains(set3.Name)) Then
                        list.Add(New RemovedRuleSetAction(set3))
                    End If
                Next
            End If
            Return list
        End Function

        Friend Function GenerateRuleSetName() As String
            Dim str2 As String
            Dim newRuleSetName As String = Messages.NewRuleSetName
            Dim num As Integer = 1
            Do
                str2 = (newRuleSetName & num.ToString(CultureInfo.InvariantCulture))
                num += 1
            Loop While MyBase.Contains(str2)
            Return str2
        End Function

        Protected Overrides Function GetKeyForItem(ByVal item As RuleSet) As String
            Return item.Name
        End Function

        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As RuleSet)
            If Me._runtimeInitialized Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            If (((Not item.Name Is Nothing) AndAlso (item.Name.Length >= 0)) AndAlso MyBase.Contains(item.Name)) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.RuleSetExists, New Object() { item.Name }))
            End If
            MyBase.InsertItem(index, item)
        End Sub

        Friend Sub OnRuntimeInitialized()
            SyncLock Me.syncLock
                If Not Me._runtimeInitialized Then
                    Dim set As RuleSet
                    For Each set In Me
                        [set].OnRuntimeInitialized
                    Next
                    Me._runtimeInitialized = True
                End If
            End SyncLock
        End Sub

        Protected Overrides Sub RemoveItem(ByVal index As Integer)
            If Me._runtimeInitialized Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            MyBase.RemoveItem(index)
        End Sub

        Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As RuleSet)
            If Me._runtimeInitialized Then
                Throw New InvalidOperationException(SR.GetString("Error_CanNotChangeAtRuntime"))
            End If
            MyBase.SetItem(index, item)
        End Sub


        ' Properties
        Friend Property RuntimeMode As Boolean
            Get
                Return Me._runtimeInitialized
            End Get
            Set(ByVal value As Boolean)
                Me._runtimeInitialized = value
            End Set
        End Property


        ' Fields
        Private _runtimeInitialized As Boolean
        <NonSerialized> _
        Private [syncLock] As Object = New Object
    End Class
End Namespace

